package gwtadmin.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;

public class AddressBar extends HorizontalPanel implements ClickHandler {
	
	private Label lbl = new Label();
	private int linkCount = 0;
	private Hyperlink[] links = { null, null, null, null, null, null, null, null };
	private IWsdlManager wsdlMan;

	public AddressBar(IWsdlManager wsdlManager) {
		wsdlMan = wsdlManager;
		addStyleName("decorFrame");
		addStyleName("addressBar");
		add(lbl);
	}
	
	private void setAddress(String[] addr) {
		
		clear();
		linkCount = addr.length;
		
		for (int i = 0; i < addr.length-1; i++) {
			Hyperlink hl = new Hyperlink(addr[i], "");
			hl.addClickHandler(this);
			
			add(hl);
			links[i] = hl;
			
			Label lbl = new Label(">");
			lbl.getElement().setAttribute("style", "margin: 0px 5px 0px 5px");
			add(lbl);
		}
		
		add(new Label(addr[addr.length-1]));
	}
	
	public void setAddress(String addr) {
		
		String[] s = null;
		
		if (addr.indexOf("/") != -1) {
			s = addr.split("/");
		} else if (addr.indexOf(" > ") != -1) {
			s = addr.split(" > ");
		} else {
			s = new String[1];
			s[0] = addr;
		}
		
		setAddress(s);
//		lbl.setText(addr);
	}
	
	private int getIndex(Hyperlink hl) {
		for (int i = 0; i < linkCount; i++) {
			if (links[i] == hl)
				return i;
		}
		return -1;
	}

	@Override
	public void onClick(ClickEvent event) {
		Hyperlink hl = (Hyperlink)event.getSource();
		
		int ind = getIndex(hl);
		
		if (hl.getText().equals("WSDL") && ind == 1) {
			wsdlMan.goListWsdl();
		} else if (hl.getText().equals("Schema") && ind == 1) {
			wsdlMan.goListSchema();
		} else if (hl.getText().equals("Policy") && ind == 1) {
			wsdlMan.goListPolicy();
		} else if (hl.getText().equals("WSDL Tree")) {
			wsdlMan.goWsdlTree();
		} else if (hl.getText().equals("Upload WSDL") && ind == 1) {
			wsdlMan.goUploadWsdl();
		} else if (hl.getText().equals("Upload Schema") && ind == 1) {
			wsdlMan.goUploadSchema();
		} else if (hl.getText().equals("Upload Policy") && ind == 1) {
			wsdlMan.goUploadPolicy();
		} else if (hl.getText().equals("Search")) {
			wsdlMan.goSearch();
		} else if (ind == 0) {
			wsdlMan.goHome();
		}
	}
}
